"use strict";

exports.__esModule = true;
exports.BaseCom = undefined;

var _net = require("../net");

var _tpl = require("./tpl");

var _tpl2 = _interopRequireDefault(_tpl);

var _wx = require("./wx");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var BaseCom = exports.BaseCom = function () {
    function BaseCom() {
        _classCallCheck(this, BaseCom);

        Object.defineProperty(this, "ele", {
            enumerable: true,
            writable: true,
            value: {}
        });
        Object.defineProperty(this, "data", {
            enumerable: true,
            writable: true,
            value: {}
        });
        Object.defineProperty(this, "properties", {
            enumerable: true,
            writable: true,
            value: {}
        });
        Object.defineProperty(this, "exports", {
            enumerable: true,
            writable: true,
            value: {}
        });
        Object.defineProperty(this, "methods", {
            enumerable: true,
            writable: true,
            value: {
                $buildUrl: function $buildUrl(url, query) {
                    return (0, _net.buildUrl)(url, query);
                },

                _setByKey: function _setByKey(k, v) {
                    var _setData;

                    return this.setData((_setData = {}, _setData[k] = v, _setData));
                },
                _getComponentById: function _getComponentById(id) {
                    return this.selectComponent("#" + id);
                },
                $setByKey: function $setByKey(k, v) {
                    var _setData2;

                    return this.setData((_setData2 = {}, _setData2[k] = v, _setData2));
                },
                $getComponentById: function $getComponentById(id) {
                    return this.selectComponent("#" + id);
                },
                $setDataTrueIfOldIsFase: function $setDataTrueIfOldIsFase(k) {
                    var self = this;
                    if (!self.data[k]) {
                        var _self$setData;

                        self.setData((_self$setData = {}, _self$setData[k] = true, _self$setData));
                    }
                },
                $toast: function $toast() {
                    var title = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
                    var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

                    (0, _wx.showToast)(Object.assign({
                        title: title
                    }, opt));
                },
                $toggleLoading: function $toggleLoading(show) {
                    if (show) {
                        (0, _wx.showLoading)();
                    } else {
                        (0, _wx.hideLoading)();
                    }
                },
                $getSettingByKey: function $getSettingByKey(key) {
                    return new Promise(function (resolve, reject) {
                        (0, _wx.getSetting)().then(function (res) {
                            if (res.authSetting && res.authSetting.hasOwnProperty(key)) {
                                resolve(res.authSetting[key]);
                            } else {
                                resolve(null);
                            }
                        })["catch"](function (e) {
                            reject(e);
                        });
                    });
                },
                $getShareAppMessage: function $getShareAppMessage(title, path) {
                    var opt = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { imageUrl: '' };

                    return Object.assign({
                        title: title,
                        path: path
                    }, opt);
                },
                $template: function $template() {
                    return _tpl2["default"].apply(undefined, arguments);
                }
            }
        });
    }

    BaseCom.prototype.__attachRefs = function __attachRefs(context) {
        var self = this;
        var sys = (0, _wx.getSystemInfoSync)();
        self.data = {
            $sys: sys
        };
        self.refs && self.refs.forEach(function (n) {
            self.ele[n] = context.selectComponent("#" + n);
        });
    };

    BaseCom.prototype._buildUrl = function _buildUrl(url, query) {
        return (0, _net.buildUrl)(url, query);
    };

    return BaseCom;
}();